package com.angel.bo.common.util.stomp;

import com.angel.bo.common.handler.stomp.StompReceiveHandler;
import com.angel.bo.common.handler.stomp.StompSessionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class StompClient {

    private String stompUrl;
    private String token;
    private StompReceiveHandler stompReceiveHandler;
    private CountDownLatch latch;

    private StompClient(String stompUrl, String token, StompReceiveHandler stompReceiveHandler, CountDownLatch latch){
        this.stompUrl = stompUrl;
        this.stompReceiveHandler = stompReceiveHandler;
        this.token = token;
        this.latch = latch;
    }

    public StompClient(String stompUrl, String token, StompReceiveHandler stompReceiveHandler){
        this.stompUrl = stompUrl;
        this.stompReceiveHandler = stompReceiveHandler;
        this.token = token;
        run();
    }

    private StompClient(){

    }

    public void run(){
        try{
            connect(stompUrl,token,stompReceiveHandler);
        }catch (Exception e){
            latch.countDown();
        }

    }

    public void connect(String stompUrl, String token, StompReceiveHandler stompReceiveHandler){

        try {
            latch = new CountDownLatch(1);
            List<Transport> transports = new ArrayList<>(1);
            StandardWebSocketClient standardWebSocketClient = new StandardWebSocketClient();
            transports.add(new WebSocketTransport(standardWebSocketClient));
            WebSocketClient transport = new SockJsClient(transports);
            WebSocketStompClient stompClient = new WebSocketStompClient(transport);

            stompClient.setMessageConverter(new StringMessageConverter());
            ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
            taskScheduler.afterPropertiesSet();
            stompClient.setTaskScheduler(taskScheduler); // for heartbeats

            StompHeaders headers = new StompHeaders();
            headers.set("Authorization",token);
            stompClient.connect(stompUrl, (WebSocketHttpHeaders) null,headers, new StompSessionHandler(stompReceiveHandler,latch));

            latch.await();
        }
        catch (Exception e) {
            log.error("链接消息服务中心异常：" ,e);
            e.printStackTrace();
        }
    }



}
